<?php
class Admin_CategoryController extends Zendvn_Controller_Action {

  // Mang tham so nhan duoc o moi Action
  protected $_arrParam;

  // Duong dan cua Controller
  protected $_currentController;

  // Duong dan cua Action chinh
  protected $_actionMain;

  // Thong so phan trang
  protected $_paginator = array('itemCountPerPage' => 5, 'pageRange' => 3);
  protected $_namespace;

  public function init() {

    // Mang tham so nhan duoc o moi Action
    $this->_arrParam = $this->_request->getParams();

    // Duong dan cua Controller
    $this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];

    // Duong dan cua Action chinh
    $this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';

    $this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
    $this->_arrParam['paginator'] = $this->_paginator;

    // Luu cac du lieu filter vao SESSION
    // Dat ten SESSION
    $this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
    $ssFilter = new Zend_Session_Namespace($this->_namespace);
    // $ssFilter->unsetAll();
    if (empty($ssFilter->col)) {
      $ssFilter->keywords = '';
    }
    $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;

    // Truyen ra view
    $this->view->arrParam = $this->_arrParam;
    $this->view->currentController = $this->_currentController;
    $this->view->actionMain = $this->_actionMain;

    $template_path = TEMPLATE_PATH . "/admin/system";
    $this->loadTemplate($template_path, 'template.ini', 'template');
  }

  public function indexAction() {

    $this->view->Title = 'Category manager :: List';
    $this->view->headTitle($this->view->Title, true);

    $tblCategory = new Admin_Model_Category();
    $this->view->Items = $tblCategory->listItem($this->_arrParam, array('task' => 'category-list'));

  }

  public function addAction() {

    $this->view->Title = 'Category manager :: Add';
    $this->view->headTitle($this->view->Title, true);
    $tblCategory = new Admin_Model_Category();
    $this->view->slbCategory = $tblCategory->itemInSelectbox();

    if ($this->_request->isPost()) {
      $validator = new Admin_Form_ValidateCategory($this->_arrParam);
      if ($validator->isError() == true) {
        $this->view->messageError = $validator->getMessageError();
        $this->view->Item = $validator->getData();
      } else {
        $tblCategory = new Admin_Model_Category();
        $arrParam = $validator->getData(array('upload' => true));
        $tblCategory->saveItem($arrParam, array('task' => 'category-add'));
        $this->_redirect($this->_actionMain);
      }
    }
  }

  public function editAction() {
    $this->view->Title = 'Category manager :: Edit';
    $this->view->headTitle($this->view->Title, true);
    $tblCategory = new Admin_Model_Category();
    $this->view->Item = $tblCategory->getItem($this->_arrParam, array('task' => 'category-edit'));

    $this->view->slbCategory = $tblCategory->itemInSelectbox($this->_arrParam, array('task' => 'category-edit'));
    if ($this->_request->isPost()) {
      $tblCategory->saveItem($this->_arrParam, array('task' => 'category-edit'));
      $this->_redirect($this->_actionMain);
    }
  }

  public function deleteAction() {
    $this->view->Title = 'Category manager :: Delete';
    $this->view->headTitle($this->view->Title, true);
    if ($this->_request->isPost()) {
      $tblCategory = new Admin_Model_Category();
      $tblCategory->deleteItem($this->_arrParam, array('task' => 'category-delete'));
      $this->_redirect($this->_actionMain);
    }
  }

  public function sortAction() {
    if ($this->_request->isPost()) {
      $tblCategory = new Admin_Model_Category();
      $tblCategory->sortItem($this->_arrParam);
      $this->_redirect($this->_actionMain);
    }
    $this->_helper->viewRenderer->setNoRender();
  }

  public function multiDeleteAction() {
    if ($this->_request->isPost()) {
      $tblCategory = new Admin_Model_Category();
      $tblCategory->deleteItem($this->_arrParam, array('task' => 'category-multi-delete'));
      $this->_redirect($this->_actionMain);
    }
    $this->_helper->viewRenderer->setNoRender();
  }

  public function statusAction() {
    $tblCategory = new Admin_Model_Category();
    $tblCategory->changeStatus($this->_arrParam, null);
    $this->_redirect($this->_actionMain);
    $this->_helper->viewRenderer->setNoRender();
  }
}
